<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>前端水印的最佳实践 | Hexo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="以背景图片的方式（推荐使用）注意事项：  canvas旋转角度的问题，横向为x轴，竖向为y轴 canvas旋转是以（0,0）点为中心，将整个画布进行旋转，通过translate可以改变画布旋转的中心点； 通过ctx.textAlign &#x3D; ‘center’ 实现水平中心对齐 通过ctx.textBaseline &#x3D; ‘middle’   实现垂直中心对齐 解决水印覆盖导致页面文字无法复制的问题：p">
<meta property="og:type" content="article">
<meta property="og:title" content="前端水印的最佳实践">
<meta property="og:url" content="http://example.com/2021/03/16/my-new-post/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="以背景图片的方式（推荐使用）注意事项：  canvas旋转角度的问题，横向为x轴，竖向为y轴 canvas旋转是以（0,0）点为中心，将整个画布进行旋转，通过translate可以改变画布旋转的中心点； 通过ctx.textAlign &#x3D; ‘center’ 实现水平中心对齐 通过ctx.textBaseline &#x3D; ‘middle’   实现垂直中心对齐 解决水印覆盖导致页面文字无法复制的问题：p">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2021-03-16T06:46:29.000Z">
<meta property="article:modified_time" content="2021-03-16T07:04:21.534Z">
<meta property="article:author" content="John Doe">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/atom.xml" title="Hexo" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/css/style.css">

  
    
<link rel="stylesheet" href="/fancybox/jquery.fancybox.min.css">

  
<meta name="generator" content="Hexo 5.4.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">Hexo</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://example.com"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-my-new-post" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/2021/03/16/my-new-post/" class="article-date">
  <time class="dt-published" datetime="2021-03-16T06:46:29.000Z" itemprop="datePublished">2021-03-16</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="p-name article-title" itemprop="headline name">
      前端水印的最佳实践
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <p><strong>以背景图片的方式（推荐使用）</strong><br>注意事项：</p>
<ol>
<li>canvas旋转角度的问题，横向为x轴，竖向为y轴</li>
<li>canvas旋转是以（0,0）点为中心，将整个画布进行旋转，通过translate可以改变画布旋转的中心点；</li>
<li>通过ctx.textAlign = ‘center’ 实现水平中心对齐</li>
<li>通过ctx.textBaseline = ‘middle’   实现垂直中心对齐</li>
<li>解决水印覆盖导致页面文字无法复制的问题：pointer-events:’none’;</li>
<li>解决打印时，取消勾选背景图片时水印消失的问题：-webkit-print-color-adjust:’exact’;(需要显示哪里的背景就加在其父元素上)<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">createWaterMark</span>(<span class="params">dom,_config</span>) </span>&#123;</span><br><span class="line">        <span class="keyword">const</span> span = <span class="built_in">document</span>.createElement(<span class="string">&#x27;span&#x27;</span>);</span><br><span class="line">        <span class="built_in">document</span>.querySelector(<span class="string">&#x27;body&#x27;</span>).append(span);</span><br><span class="line">        span.innerHTML = _config.txt;</span><br><span class="line">        span.style.fontSize = <span class="string">`<span class="subst">$&#123;_config.fontSize&#125;</span>px`</span>;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">let</span> spanWidth = span.offsetWidth;</span><br><span class="line">        <span class="keyword">const</span> canvas = <span class="built_in">document</span>.createElement(<span class="string">&#x27;canvas&#x27;</span>);</span><br><span class="line">        canvas.width = spanWidth &gt; <span class="number">380</span> ? spanWidth : <span class="number">380</span>;</span><br><span class="line">        canvas.height = spanWidth &gt; <span class="number">500</span> ? spanWidth + <span class="number">200</span> : <span class="number">500</span>;</span><br><span class="line">        <span class="keyword">const</span> ctx = canvas.getContext(<span class="string">&#x27;2d&#x27;</span>);</span><br><span class="line">        ctx.fillStyle = _config.fillStyle;</span><br><span class="line">        ctx.font = _config.font;</span><br><span class="line">        <span class="keyword">let</span> ox = canvas.width / <span class="number">2</span>,</span><br><span class="line">            oy = canvas.height / <span class="number">2</span>;</span><br><span class="line">        ctx.translate(ox, oy); <span class="comment">//移动至中心</span></span><br><span class="line">        ctx.rotate(<span class="built_in">Math</span>.PI / <span class="number">180</span> * _config.angle); <span class="comment">//旋转角度</span></span><br><span class="line">        ctx.textAlign = <span class="string">&#x27;center&#x27;</span>;</span><br><span class="line">        ctx.textBaseline = <span class="string">&#x27;middle&#x27;</span>; <span class="comment">//绘制文本的基线</span></span><br><span class="line">        ctx.fillText(_config.txt, <span class="number">0</span>, <span class="number">0</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 重置画布中心</span></span><br><span class="line">        <span class="comment">// ctx.translate(-ox, -oy);</span></span><br><span class="line">        <span class="keyword">const</span> url = canvas.toDataURL();</span><br><span class="line">        <span class="keyword">const</span> watermakr = <span class="built_in">document</span>.createElement(<span class="string">&#x27;div&#x27;</span>);<span class="comment">//创建div,设置其背景</span></span><br><span class="line">        watermakr.style.position = <span class="string">&#x27;absolute&#x27;</span>; <span class="comment">//相对于父元素位置</span></span><br><span class="line">        watermakr.style.top = <span class="number">0</span>;</span><br><span class="line">        watermakr.style.left = <span class="number">0</span>;</span><br><span class="line">        watermakr.style.width = <span class="string">`calc(100%)`</span>;</span><br><span class="line">        watermakr.style.height = <span class="string">`100%`</span>;</span><br><span class="line">        watermakr.style.pointerEvents = <span class="string">&#x27;none&#x27;</span>; <span class="comment">//解决无法复制文字问题</span></span><br><span class="line">        watermakr.style.backgroundImage = <span class="string">`url(<span class="subst">$&#123;url&#125;</span>)`</span></span><br><span class="line">        watermakr.style.webkitPrintColorAdjust = <span class="string">&#x27;exact&#x27;</span>; <span class="comment">//防止浏览器打印时背景图片丢失</span></span><br><span class="line">        <span class="built_in">document</span>.querySelector(<span class="string">`<span class="subst">$&#123;dom&#125;</span>`</span>).appendChild(watermakr)</span><br><span class="line">        span.style.display = <span class="string">&#x27;none&#x27;</span>; <span class="comment">//因此用来计算文本长度的span</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">const</span> _config = &#123;</span><br><span class="line">        fillStyle: <span class="string">&#x27;rgba(0,0,0,0.5)&#x27;</span>, <span class="comment">// 背景色</span></span><br><span class="line">        font: <span class="string">&#x27;16px serif&#x27;</span>,</span><br><span class="line">        waterColor: <span class="string">&#x27;blue&#x27;</span>,</span><br><span class="line">        angle:-<span class="number">45</span>,</span><br><span class="line">        txt:<span class="string">&#x27;水印测试水印测试水印测试水印测试&#x27;</span>,</span><br><span class="line">    &#125;</span><br><span class="line">    createWaterMark(<span class="string">&#x27;.watermark&#x27;</span>,_config)</span><br></pre></td></tr></table></figure></li>
</ol>
<p><strong>以图片的方式</strong></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">drawWaterMark</span>(<span class="params"></span></span></span><br><span class="line"><span class="function"><span class="params">        text = <span class="string">&#x27;车300伽马风控&#x27;</span>,</span></span></span><br><span class="line"><span class="function"><span class="params">        d = <span class="number">2</span>,</span></span></span><br><span class="line"><span class="function"><span class="params">        fontSize = <span class="number">24</span>,</span></span></span><br><span class="line"><span class="function"><span class="params">        dom = <span class="string">&#x27;body&#x27;</span>,</span></span></span><br><span class="line"><span class="function"><span class="params">        _config,</span></span></span><br><span class="line"><span class="function"><span class="params">        supportPrint = <span class="literal">false</span>, <span class="comment">//是否支持打印</span></span></span></span><br><span class="line"><span class="function"><span class="params">        callback</span></span></span><br><span class="line"><span class="function"><span class="params">    </span>) </span>&#123;</span><br><span class="line">        <span class="keyword">const</span> config = &#123;</span><br><span class="line">            fontWeight: _config.fontWeight || <span class="string">&#x27;bold&#x27;</span>, <span class="comment">// 加粗</span></span><br><span class="line">            fontFamily: _config.fontFamily || <span class="string">&#x27;黑体&#x27;</span>, <span class="comment">// 字体</span></span><br><span class="line">            fillStyle: _config.fillStyle || <span class="string">&#x27;rgba(0,0,0,0.5)&#x27;</span>, <span class="comment">// 背景色</span></span><br><span class="line">            canvasWidth: _config.canvasWidth || <span class="number">600</span>, <span class="comment">// canvas 宽</span></span><br><span class="line">            canvasHeight: _config.canvasHeight || <span class="number">600</span>, <span class="comment">// canvas 高</span></span><br><span class="line">            textPostionX: _config.textPostionX || <span class="number">80</span>, <span class="comment">// 文字位置 x轴</span></span><br><span class="line">            textPostionY: _config.textPostionY || <span class="number">20</span>, <span class="comment">// 文字位置 y轴</span></span><br><span class="line">            <span class="comment">//  zIndex: _config.zIndex || 0, // 层叠等级</span></span><br><span class="line">            showDouble: _config.showDouble || <span class="literal">false</span> <span class="comment">// 是否增加水印密度</span></span><br><span class="line">        &#125;;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//判断水印文本长度</span></span><br><span class="line">        <span class="keyword">var</span> SPAN = <span class="built_in">document</span>.createElement(<span class="string">&#x27;span&#x27;</span>);</span><br><span class="line">        <span class="built_in">document</span>.querySelector(<span class="string">&#x27;body&#x27;</span>).append(SPAN);</span><br><span class="line">        SPAN.innerHTML = text;</span><br><span class="line">        SPAN.style.fontSize = <span class="string">`<span class="subst">$&#123;fontSize&#125;</span>px`</span>;</span><br><span class="line">        <span class="keyword">let</span> spanWidth = SPAN.offsetWidth;</span><br><span class="line">        <span class="built_in">document</span>.querySelector(<span class="string">&#x27;.watermark&#x27;</span>).style.width = spanWidth + <span class="string">&#x27;px&#x27;</span>;</span><br><span class="line">        <span class="built_in">document</span>.querySelector(<span class="string">&#x27;.watermark&#x27;</span>).style.height = spanWidth + <span class="string">&#x27;px&#x27;</span>;</span><br><span class="line">        <span class="keyword">let</span> body;</span><br><span class="line">        <span class="keyword">if</span> (dom) &#123;</span><br><span class="line">            body = <span class="built_in">document</span>.querySelector(dom);</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            body = <span class="built_in">document</span>.querySelector(<span class="string">&#x27;body&#x27;</span>);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">const</span> waterMarkDiv = <span class="built_in">document</span>.createElement(<span class="string">&#x27;div&#x27;</span>); <span class="comment">// 渲染水印的 div</span></span><br><span class="line">        waterMarkDiv.style.overflow = <span class="string">&#x27;hidden&#x27;</span>;</span><br><span class="line">        waterMarkDiv.style.width = <span class="string">`calc(100%)`</span>;</span><br><span class="line">        waterMarkDiv.style.height = <span class="string">`100%`</span>;</span><br><span class="line">        waterMarkDiv.style.position = <span class="string">&#x27;absolute&#x27;</span>;</span><br><span class="line">        waterMarkDiv.style.top = <span class="number">0</span>;</span><br><span class="line">        waterMarkDiv.style.left = <span class="number">0</span>;</span><br><span class="line">        waterMarkDiv.style.pointerEvents = <span class="string">&#x27;none&#x27;</span>; <span class="comment">//解决无法复制文字问题</span></span><br><span class="line">        waterMarkDiv.style.display = <span class="string">&#x27;flex&#x27;</span>; <span class="comment">//这边要使用弹性布局</span></span><br><span class="line">        waterMarkDiv.style.flexWrap = <span class="string">&#x27;wrap&#x27;</span>;</span><br><span class="line">        waterMarkDiv.style.justifyContent = <span class="string">&#x27;space-between&#x27;</span>;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">const</span> canvas = <span class="built_in">document</span>.createElement(<span class="string">&#x27;canvas&#x27;</span>);</span><br><span class="line">        <span class="comment">//根据水印的长度 计算canvas的宽高</span></span><br><span class="line">        canvas.width = spanWidth; <span class="comment">//宽度增加一部分是为了避免水印倾斜时被遮挡</span></span><br><span class="line">        canvas.height = spanWidth;</span><br><span class="line">        canvas.style.display = <span class="string">&#x27;none&#x27;</span>;</span><br><span class="line">        body.append(canvas);</span><br><span class="line">        <span class="keyword">const</span> ctx = canvas.getContext(<span class="string">&#x27;2d&#x27;</span>);</span><br><span class="line">        ctx.clearRect(<span class="number">0</span>, <span class="number">0</span>, <span class="built_in">window</span>.innerWidth, <span class="built_in">window</span>.innerHeight); <span class="comment">//clearRect方法清空给定矩形内的指定像素。</span></span><br><span class="line">        ctx.font = <span class="string">`<span class="subst">$&#123;fontSize&#125;</span>px <span class="subst">$&#123;config.fontWeight&#125;</span> <span class="subst">$&#123;config.fontFamily&#125;</span>`</span>; <span class="comment">// &#x27;24px bold 黑体&#x27;</span></span><br><span class="line">        ctx.fillStyle = config.fillStyle; <span class="comment">// &#x27;rgba(0,0,0,0.1)&#x27;</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//旋转到画布中心再旋转</span></span><br><span class="line">        ctx.translate(spanWidth / <span class="number">2</span>, spanWidth / <span class="number">2</span>); <span class="comment">//改变画布的中心</span></span><br><span class="line">        ctx.rotate((<span class="built_in">Math</span>.PI / <span class="number">180</span>) * d); <span class="comment">//旋转角度</span></span><br><span class="line">        ctx.textAlign = <span class="string">&#x27;center&#x27;</span>;</span><br><span class="line">        ctx.textBaseline = <span class="string">&#x27;middle&#x27;</span>; <span class="comment">//填充文本对齐方式</span></span><br><span class="line">        ctx.fillText(text, <span class="number">0</span>, <span class="number">0</span>); <span class="comment">//填充水印文本</span></span><br><span class="line">        body.append(waterMarkDiv);</span><br><span class="line">        <span class="keyword">let</span> row = <span class="built_in">Math</span>.ceil(body.offsetWidth / spanWidth); <span class="comment">//大致每行的个数</span></span><br><span class="line">        <span class="keyword">let</span> clo = <span class="built_in">Math</span>.ceil(body.offsetHeight / spanWidth); <span class="comment">//多少列</span></span><br><span class="line">        <span class="built_in">console</span>.log(row,clo)</span><br><span class="line">        <span class="comment">//如果需要打印</span></span><br><span class="line">        <span class="keyword">if</span> (supportPrint) &#123;</span><br><span class="line">            <span class="comment">// 是为了保证在打印时水印的个数足够，因为打印页面会缩放，y轴高度会变大</span></span><br><span class="line">            <span class="comment">//将生成的水印以图片的形式平铺填充在div中</span></span><br><span class="line">            <span class="keyword">var</span> pic = canvas.toDataURL(<span class="string">&#x27;image/png&#x27;</span>);</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; <span class="built_in">Math</span>.ceil(clo * row) + row; i++) &#123;</span><br><span class="line">                <span class="comment">//多加一列，保证最后一行能够被水印完全覆盖</span></span><br><span class="line">                <span class="keyword">const</span> img = <span class="built_in">document</span>.createElement(<span class="string">&#x27;img&#x27;</span>);</span><br><span class="line">                img.style.width = spanWidth;</span><br><span class="line">                img.style.height = spanWidth;</span><br><span class="line">                img.setAttribute(<span class="string">&#x27;src&#x27;</span>, <span class="string">`<span class="subst">$&#123;pic&#125;</span>`</span>);</span><br><span class="line">                waterMarkDiv.append(img);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="comment">//本地环境的</span></span><br><span class="line">            waterMarkDiv.style.setProperty(</span><br><span class="line">                <span class="string">&#x27;background&#x27;</span>,</span><br><span class="line">                <span class="string">`url(<span class="subst">$&#123;canvas.toDataURL(<span class="string">&#x27;image/png&#x27;</span>)&#125;</span>)`</span>,</span><br><span class="line">                <span class="string">&#x27;important&#x27;</span></span><br><span class="line">            );</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="built_in">document</span>.getElementsByTagName(<span class="string">&#x27;body&#x27;</span>)[<span class="number">0</span>].style.zoom = <span class="number">1</span>;</span><br><span class="line">        SPAN.style.display = <span class="string">&#x27;none&#x27;</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    drawWaterMark(<span class="string">&#x27;测试测试测试测试测试测试测试测试&#x27;</span>,-<span class="number">45</span>,<span class="number">35</span>,<span class="string">&#x27;.watermark&#x27;</span>,&#123;&#125;,<span class="literal">true</span>)</span><br></pre></td></tr></table></figure>
<p><strong>svg（也是以背景图片为基础）</strong></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">createWaterMark</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line"><span class="comment">//这里的canvas的宽高最好也要和水印的长度保持一致</span></span><br><span class="line">      <span class="keyword">const</span> svgStr =</span><br><span class="line">        <span class="string">`&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;112px&quot; height=&quot;112px&quot;&gt;</span></span><br><span class="line"><span class="string">          &lt;text  stroke=&quot;#000000&quot;</span></span><br><span class="line"><span class="string">          transform=&quot;translate(56,56)</span></span><br><span class="line"><span class="string">          rotate(-45)&quot;</span></span><br><span class="line"><span class="string">          alignment-baseline=&quot;middle&quot;</span></span><br><span class="line"><span class="string">          text-anchor=&quot;middle&quot;</span></span><br><span class="line"><span class="string">          opacity=&quot;0.8&quot;</span></span><br><span class="line"><span class="string">          font-weight=&quot;100&quot;</span></span><br><span class="line"><span class="string">          font-size=&quot;16&quot;</span></span><br><span class="line"><span class="string">          font-family=&quot;microsoft yahe&quot;</span></span><br><span class="line"><span class="string">          &gt;</span></span><br><span class="line"><span class="string">                  测试水印</span></span><br><span class="line"><span class="string">          &lt;/text&gt;</span></span><br><span class="line"><span class="string">        &lt;/svg&gt;`</span>;</span><br><span class="line">      <span class="keyword">return</span> <span class="string">`data:image/svg+xml;base64,<span class="subst">$&#123;<span class="built_in">window</span>.btoa(<span class="built_in">unescape</span>(<span class="built_in">encodeURIComponent</span>(svgStr)))&#125;</span>`</span>;</span><br><span class="line">			&#125;</span><br><span class="line">        <span class="keyword">const</span> watermakr = <span class="built_in">document</span>.createElement(<span class="string">&#x27;div&#x27;</span>);</span><br><span class="line">        watermakr.style.width = <span class="string">&#x27;500px&#x27;</span>;</span><br><span class="line">        watermakr.style.height = <span class="string">&#x27;500px&#x27;</span>;</span><br><span class="line">        watermakr.className = <span class="string">&#x27;watermark&#x27;</span>;</span><br><span class="line">        watermakr.style.backgroundImage = <span class="string">`url(<span class="subst">$&#123;createWaterMark()&#125;</span>)`</span></span><br><span class="line">        <span class="built_in">document</span>.body.appendChild(watermakr)</span><br></pre></td></tr></table></figure>
      
    </div>
    <footer class="article-footer">
      <a data-url="http://example.com/2021/03/16/my-new-post/" data-id="ckmbrvn1h00023ot94fd4h40q" data-title="前端水印的最佳实践" class="article-share-link">Share</a>
      
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2021/03/16/js%E9%81%8D%E5%8E%86%E7%9A%84%E5%87%A0%E7%A7%8D%E6%96%B9%E5%BC%8F/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          js遍历的几种方式
        
      </div>
    </a>
  
  
    <a href="/2021/03/16/hello-world/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">Hello World</div>
    </a>
  
</nav>

  
</article>


</section>
        
          <aside id="sidebar">
  
    

  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/03/">March 2021</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2021/03/16/%E5%89%8D%E7%AB%AF%E6%9C%AC%E5%9C%B0%E5%BC%80%E5%8F%91%E7%8E%AF%E5%A2%83%E7%9A%84%E6%90%AD%E5%BB%BA/">前端需要的开发环境</a>
          </li>
        
          <li>
            <a href="/2021/03/16/js%E9%81%8D%E5%8E%86%E7%9A%84%E5%87%A0%E7%A7%8D%E6%96%B9%E5%BC%8F/">js遍历的几种方式</a>
          </li>
        
          <li>
            <a href="/2021/03/16/my-new-post/">前端水印的最佳实践</a>
          </li>
        
          <li>
            <a href="/2021/03/16/hello-world/">Hello World</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      
      &copy; 2021 John Doe<br>
      Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="/js/jquery-3.4.1.min.js"></script>



  
<script src="/fancybox/jquery.fancybox.min.js"></script>




<script src="/js/script.js"></script>





  </div>
</body>
</html>